package RunnableOldClass;


import file.fileread.*;
import RunableClass.useClass.Addport;
import Alert.ST_name;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import STOCK.*;
import java.io.File;
import javax.swing.table.DefaultTableModel;
import mysql.connect;
/**
 *
 * @author javadb.com
 */
public class ReadDataA  implements Runnable {
String symbolx;int Block[]=new int[12];
public static String[]sym=new String[12];String x ="";
    private DefaultTableModel model1;

                   
    /**
     * Reads text from a file line by line
     */
    public String[] readFromFile(String filename,String symbol) throws SQLException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya;
       for(int i=0;i<sym.length;i++){
          // sym[i]=null;
       }
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new FileReader(filename));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                  int count1 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                            http://www.settrade.com/C13_FastQuote_Main.jsp?txtSymbol=ASIAN
                    if(line.contains("C13_FastQuote_Main.jsp?txtSymbol="+symbol+"\"")){
                            for(int i=0;i<5;i++){
                                char xx=line.charAt(i);
                                
                                if(xx>='0'&&xx<='9'){
                                    linenumber+=line.substring(i, i+1);       
                                }        
                            } //System.out.print(linenumber);  //System.out.println(aa);
                       if(line.startsWith(linenumber)){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(isDigit(line.substring(b[i]+1,c[i]))){
                                 ya=line.substring(b[i]+1,c[i]);
                                 if(ya.equals(symbol)){
                                     sym[0]=ya;
                                     
                                 }
                             }
                        } }
                  //  x+="@"+y;
                    

                    for(int k=1;k<=11;k++){
                        Block[k]=Integer.parseInt(linenumber)+k;
                        
                        
                        
                    }
                     }
                    // System.out.println(Block[1]);
                   
                            ////////////////////////////////////
                    // System.out.print(""+y);
                     } 
                        for(int k=1;k<=11;k++){
                       
                        if(line.startsWith(String.valueOf(Block[k])+"-->")){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(isDigit(line.substring(b[i]+1,c[i]))){              
                                      xc=line.substring(b[i]+1,c[i]);
                                      
                             }}
                             }

                        if((!xc.equals("XD")||!xc.equals("SP")||!xc.equals("NP")||!xc.equals("SP,NP,NC")||!xc.equals("XW")
                           ||!xc.equals("SP,NC")||!xc.equals("SP,NP")||!xc.equals("NP,NC" ))&& k==1){


                           y="NOTVALUE";
                          
                       }else{
                           y=xc;
                           
                       }
                        
                    //x+="@"+y;
                  sym[k]=y;
                   if(k==11){
                    break;

                           }
                     //  System.out.print(y);
                     }
                        
                   
                    }
                 
 
        
            
         
            }
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(ReadDataA.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return sym;
    }

    /**
     * @param args the command line arguments
     */
  /*public static void main(String[] args) throws SQLException {
      ST_name st=new ST_name();
      for(int p=0;p<ST_name.st.length;p++){
     ReadData s=  new ReadData();
   readFromFile("StockFile.txt",st.getname(p));
                                    for(int o=0;o<s.sym.length;o++){
                                        System.out.print(s.sym[o]+",");
                                    }System.out.println("");
      }                
       
  }*/
    
     public static boolean isDigit(String s)   {
     boolean right = false;
      char c = 0;
      for (int index = 0; index < s.length(); index ++)  {
            c = s.charAt(index);
            if (c >= '0' || c <= '9'||c=='.')  {
            right = true;
           }
       }
      return right;
}
  
    @Override
    public void run() {
             
          for(;;){ 
                File f=new File("StockFile.txt");
            //  System.out.println(f.length()+"->"+f.exists());
              if(f.length()>0){
            int countindex=0;
               
              try {
          String sqlsec="SELECT count(*) FROM `stock_ alert`";
                connect a=   new connect();
                  a.rs= a.stmt.executeQuery(sqlsec);
               a.rs.next();
                      
               countindex= Integer.parseInt(a.rs.getString(1));
             //   BrowserControl.displayURL(a.rs.getString("Link"));
           a.CloseDB();
          
       } catch (SQLException ex) {
       } DefaultTableModel model=new DefaultTableModel(Col1,0);
              if(countindex>0){
                  
          String Symbol[]=new String[countindex];  
              try {
          String sqlsec="SELECT * FROM `stock_ alert`";
                connect a=   new connect();
                  a.rs= a.stmt.executeQuery(sqlsec);
                  int indexarray=0;
               while(a.rs.next()){
               
                 Symbol[indexarray]=a.rs.getString("stockname");  
                 ++indexarray;
                
             //   BrowserControl.displayURL(a.rs.getString("Link"));
               }
             //  javax.swing.JOptionPane.showMessageDialog(null, Symbol[0] );
               //javax.swing.JOptionPane.showMessageDialog(null, Symbol[1] );
           a.CloseDB();
        
       } catch (SQLException ex) {
       }
              
              
              for(int i=0;i<Symbol.length;i++){
                    // javax.swing.JOptionPane.showMessageDialog(null,Symbol.length );
               //javax.swing.JOptionPane.showMessageDialog(null, Symbol[1] );
        try {
              String[] Array= this.readFromFile("StockFile.txt",Symbol[i]);
                                   // for(int o=0;o<Array.length;o++){
                                    //    System.out.print(Array[o]+",");
                                   // }
                                      //  System.out.println("");
         
      // for(int l=0;l<sym.length;l++){
          //if(sym[l].equals(Symbol[i])){
               //  javax.swing.JOptionPane.showMessageDialog(null,RD.sym[5] );
         //     System.out.print("->"+Array[7]);
               String xc[]={Array[0],Array[5],Addport.Setcolor(Array[6]).toString()};
              // System.out.println(Array[0]+"-"+Array[5]+"-"+Array[6]);
              //  System.out.println("----------------------------------------");
              model.addRow(xc); 
         //  }
              
          // System.out.println(RD.sym[0]+"-"+RD.sym[5]+"-"+RD.sym[6]);
     //  }
        } catch (SQLException ex) {
        }
              }
              
               }else{
                   String xc[]={"","",""};
              model.addRow(xc); 
              }
         STOCK.TbListAlert.setModel(model);
           STOCK.TbListAlert.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
       
        STOCK.TbListAlert.getColumnModel().getColumn(0).setResizable(false);
        STOCK.TbListAlert.getColumnModel().getColumn(0).setPreferredWidth(100);
        STOCK.TbListAlert.getColumnModel().getColumn(1).setResizable(false);
        STOCK.TbListAlert.getColumnModel().getColumn(1).setPreferredWidth(120);
        STOCK.TbListAlert.getColumnModel().getColumn(2).setResizable(true);
        STOCK.TbListAlert.getColumnModel().getColumn(2).setPreferredWidth(100);   
      
             
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    }}
    }
     String[] Col1 = {"SymbolName","Value","Chg"};
   
}



